package com.controller;

import com.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import static com.common.Constants.DATE_TIME_FORMAT;

@Slf4j
@RestController
public class HelloController {

    @GetMapping("/hello")
    public void hello(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.getWriter().write("abcdefg");
    }

    @GetMapping("/echoName")
    public void echoName(@RequestParam String name, @RequestParam int age, HttpServletResponse httpServletResponse) throws IOException {
        log.debug("{}的年龄是{}", name, age);
        httpServletResponse.getWriter().write("your name is " + name);
    }

    @PostMapping("/addUser")
    public void addUser(@RequestBody UserVo userVo, HttpServletResponse httpServletResponse) throws IOException {

        httpServletResponse.getWriter().write(userVo.getName() + "...");
    }


    @GetMapping("/userPage")
    @ResponseBody
    public List<UserVo> userPage() {


        return Arrays.asList(
                new UserVo(1, "罗俊华"),
                new UserVo(2, "其他人")
        );

    }

    @GetMapping("/page/{current}/{size}")
    public String page(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return current * size + "";
    }


    @GetMapping("/login")
    public HttpEntity<String> login(UserVo userVo) {
        return new HttpEntity<>(userVo.getName() + "登陆成功");
    }


    @GetMapping("/updateDate")
    public String updateDate(@RequestParam @DateTimeFormat(pattern = DATE_TIME_FORMAT) LocalDateTime dateTime){
        log.debug("传入的日期是{}",dateTime);
        return DateTimeFormatter.ISO_DATE_TIME.format(dateTime);
    }
}
